package cn.qdhlm.collectservice.service;

import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.helper.RocketMqProducer;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.JsonUtil;
import cn.qdhlm.collectservice.model.rocketmq.Command;
import cn.qdhlm.collectservice.model.rocketmq.OnlineRecord;
import cn.qdhlm.collectservice.model.rocketmq.TerminalData;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

import static cn.qdhlm.collectservice.common.Constant.COLLECTOR_ONLINE_TYPE_DATA;

/**
 * @Name: UtilityService
 * @Description: UtilityService
 * @Author: jiangcs
 */
@Log4j2
@Service
public class UtilityService {
    @Autowired
    private RocketMqProducer producer;
/*    @Autowired
    private OnlineCollectorMapper onlineCollectorMapper;
    @Autowired
    private CommandCollectorMapper commandCollectorMapper;
    @Autowired
    private CommandTerminalMapper commandTerminalMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private TerminalMapper terminalMapper;
    @Autowired
    private ConfigParamTerminalMapper configParamTerminalMapper;*/

    /**
     * 获取当前采暖季
     *
     * @param year：2018
     * @param monthDay：09-30
     * @return
     */
/*    public String getHeatSeason(String year, String monthDay) {
        if (ComUtil.isEmpty(year)) {
            year = ComUtil.formatDateTime(new Date(), Constant.DATE_FORMAT_YEAR);
        }
        if (ComUtil.isEmpty(monthDay)) {
            monthDay = ComUtil.formatDateTime(new Date(), Constant.DATE_FORMAT_MONTH_DAY);
        }

        String heatSeason;
        if (monthDay.compareTo(Constant.HEATING_SEASON_DATE) > 0) {
            heatSeason = year + "-" + (ComUtil.safeInt(year) + 1);
        } else {
            heatSeason = (ComUtil.safeInt(year) - 1) + "-" + year;
        }
        return heatSeason;
    }*/

    /**
     * 采集器在线数据 保存 mongodb
     *
     * @param type
     * @param collectorNo
     * @param version
     * @param simNetwork
     * @param simSignal
     */
    public SendResult sendOnlineData(int type, String collectorNo, String version, Integer simNetwork, Integer simSignal) {
        OnlineRecord onlineRecord = new OnlineRecord();
        onlineRecord.setType(type);
        onlineRecord.setCollectorNo(collectorNo);
        onlineRecord.setVersion(version);
        onlineRecord.setSimNetwork(simNetwork);
        onlineRecord.setSimSignal(simSignal);
        log.info("onlineData.tostring{}", JsonUtil.safeJsonStr(onlineRecord));
        return producer.send("OnlineData", "mqtt", JsonUtil.safeJsonStr(onlineRecord).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * @param realTime
     * @param terminalNo
     * @param collectorNo
     * @param varMap
     * @param collectStatus
     * @param terminalState
     * @param panelState
     * @param collectTime
     */
    public SendResult sendTerminalData(boolean realTime, String terminalNo, String collectorNo, Map<String, Object> varMap, int collectStatus, String terminalState, String panelState, String collectTime) {
        TerminalData terminalData = new TerminalData();
        terminalData.setRealTime(realTime);
        terminalData.setVarMap(varMap);
        terminalData.setTerminalNo(terminalNo);
        terminalData.setCollectorNo(collectorNo);
        terminalData.setCollectStatus(collectStatus);
        terminalData.setTerminalState(terminalState);
        terminalData.setPanelState(panelState);
        terminalData.setCollectTime(collectTime);

//        terminalData.setTerminalId(terminalVO.getId());
//        terminalData.setTerminalType(terminalVO.getType());
//        terminalData.setTerminalLevel(terminalVO.getLevel());
//        terminalData.setMacAddr(terminalVO.getMacAddr());
//        terminalData.setCollectorNo(terminalVO.getCollectorNo());
//        terminalData.setCustomerId(terminalVO.getCustomerId());
//        terminalData.setHouseholdId(terminalVO.getHouseholdId());
//        terminalData.setHeatState(terminalVO.getHeatState());
//        terminalData.setProtocolCode(terminalVO.getProtocolCode());
//        terminalData.setChargeFactorId(terminalVO.getChargeFactorId());
//        terminalData.setChargeWay(terminalVO.getChargeWay());
//        terminalData.setCustomerTxt(terminalVO.getCustomerTxt());

        terminalData.setStatus(0);
        terminalData.setCreateTime(new Date());
        log.info("terminalData.tostring:{}", JsonUtil.safeJsonStr(terminalData));
        // 在线
        sendOnlineData(COLLECTOR_ONLINE_TYPE_DATA,collectorNo,null,null,null);

        // 数据
        return producer.send("TerminalData", "mqtt", JsonUtil.safeJsonStr(terminalData).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 命令 保存 mongodb
     *
     * @param nextSn
     * @param collectorNo
     * @param terminalNo
     * @param funcCode
     * @param byteBuf
     * @return
     */
    public SendResult sendCommand(int nextSn, String collectorNo, String terminalNo, String funcCode, ByteBuf byteBuf) {
        Command command = new Command();
        command.setCollectorNo(collectorNo);
        command.setTerminalNo(terminalNo);
        command.setFuncCode(funcCode);
        command.setData(CodeUtil.hexStringToBase64String(ByteBufUtil.hexDump(byteBuf)));
        command.setNextSn(nextSn);
//        command.setCommandType(commandType);
//        command.setCommandCollectorId(commandCollectorId);
//        command.setCommandTerminalId(commandTerminalId);
        command.setStatus("READY");
        command.setCreateTime(new Date());
        SendResult sendResult = producer.send("Command", "mqtt", JsonUtil.safeJsonStr(command).getBytes(StandardCharsets.UTF_8));
        log.info("send command ok: {}", JsonUtil.safeJsonStr(command));
        return sendResult;
    }

    /**
     * 同步采集器在线状态
     *
     * @param type
     * @param collectorNo
     * @param collectorStatus
     * @param version
     * @param simNetwork
     * @param simSignal
     * @throws Exception
     */
/*    public void syncCollectorOnlineStatus(int type, String collectorNo, Integer collectorStatus, String version, Integer simNetwork, Integer simSignal) throws Exception {
        Integer id = onlineCollectorMapper.getOnlineIdByCollectorNo(collectorNo);
        if (ComUtil.isEmpty(id)) {
            int count = onlineCollectorMapper.insert(collectorNo, version);
            if (count <= 0) {
                log.error("collectorNo[{}], type[{}] insert col_online_collector fail", collectorNo, type);
                return;
            }
            log.info("collectorNo[{}], type[{}] insert col_online_collector[reg] ok", collectorNo, type);
        } else {
            int count;
            switch (type) {
                // type：1，注册
                case Constant.COLLECTOR_ONLINE_TYPE_REG:
                    // status=1: 平台已注册，更新活动时间、版本
                    if (collectorStatus == 1) {
                        count = onlineCollectorMapper.updateVersionAndActiveTime(id, version);
                        if (count <= 0) {
                            log.error("collectorNo[{}], type[{}] update col_online_collector version&active fail", collectorNo, type);
                            return;
                        }
                        log.info("collectorNo[{}], type[{}] update col_online_collector version&active ok", collectorNo, type);
                    }
                    // status=0,2,3: 平台已导入，更新原记录为离线，新增在线记录
                    else {
                        // 更新原记录离线
                        count = onlineCollectorMapper.updateOffline(id);
                        if (count <= 0) {
                            log.error("collectorNo[{}], type[{}] update col_online_collector offline fail", collectorNo, type);
                            return;
                        }
                        log.info("collectorNo[{}], type[{}] update col_online_collector offline ok", collectorNo, type);

                        // 新增上线记录
                        count = onlineCollectorMapper.insert(collectorNo, version);
                        if (count <= 0) {
                            log.error("collectorNo[{}], type[{}] insert col_online_collector online fail", collectorNo, type);
                            return;
                        }
                        log.info("collectorNo[{}], type[{}] insert col_online_collector online ok", collectorNo, type);
                    }
                    break;
                // type：2，心跳
                case Constant.COLLECTOR_ONLINE_TYPE_HEART:
                    // 更新活动时间、版本
                    count = onlineCollectorMapper.updateVersionAndActiveTime(id, version);
                    if (count <= 0) {
                        log.error("collectorNo[{}], type[{}] update col_online_collector version&active fail", collectorNo, type);
                        return;
                    }
                    log.info("collectorNo[{}], type[{}] update col_online_collector version&active ok", collectorNo, type);
                    break;
                // type: 3，数据
                case Constant.COLLECTOR_ONLINE_TYPE_DATA:
                    // 更新活动时间
                    count = onlineCollectorMapper.updateActiveTime(id);
                    if (count <= 0) {
                        log.error("collectorNo[{}], type[{}] update col_online_collector active fail", collectorNo, type);
                        return;
                    }
                    log.info("collectorNo[{}], type[{}] update col_online_collector active ok", collectorNo, type);
                    break;
                // type: 4，运行参数
                case Constant.COLLECTOR_ONLINE_TYPE_OPERATE_PARAM:
                    count = onlineCollectorMapper.updateOperateParam(collectorNo, simNetwork, simSignal);
                    if (count <= 0) {
                        log.error("collectorNo[{}], type[{}] update col_online_collector simNetwork[{}], simSignal[{}] fail", collectorNo, type, simNetwork, simSignal);
                        return;
                    }
                    log.info("collectorNo[{}], type[{}] update col_online_collector simNetwork[{}], simSignal[{}] ok", collectorNo, type, simNetwork, simSignal);
                default:
                    log.error("sync collector online status: type[{}] match fail", type);
            }
        }
    }*/

    /**
     * 更新 command result & status
     *
     * @param commandType
     * @param collectorNo
     * @param sn
     * @param resultMap
     * @param status
     */
    /*public void updateCommandResultStatus(String commandType, String collectorNo, int sn, Map<String, Object> resultMap, int status) {
        // 结果转换处理
        String result = JsonUtil.safeJsonStr(resultMap);
        String resultZH = JsonUtil.safeJsonStr(convert2zh(resultMap));

        Integer id;
        switch (commandType) {
            case "collector":
                id = commandCollectorMapper.getIdByCollectorNoAndSnAndStatus(collectorNo, sn, 1);
                if (!ComUtil.isEmpty(id)) {
                    int count = commandCollectorMapper.updateResultStatus(id, result, resultZH, status);
                    if (count <= 0) {
                        log.error("update commandStatus: commandCollectorMapper.updateStatus fail");
                    }
                } else {
                    log.error("update commandStatus: commandCollectorMapper.get collectorNo[{}],sn[{}],status[1] fail", collectorNo, sn);
                }
                break;
            case "terminal":
                id = commandTerminalMapper.getIdByCollectorNoAndSnAndStatus(collectorNo, sn, 1);
                if (!ComUtil.isEmpty(id)) {
                    int count = commandTerminalMapper.updateResultStatus(id, result, resultZH, status);
                    if (count <= 0) {
                        log.error("update commandStatus: commandTerminalMapper.updateStatus fail");
                    }
                } else {
                    log.error("update commandStatus: commandTerminalMapper.get collectorNo[{}],sn[{}],status[1] fail", collectorNo, sn);
                }
                break;
            default:
                log.error("update commandStatus: commandType[{}] match fail", commandType);
        }
    }*/

    /**
     * 结果 关键字 转换
     *
     * @param resultMap
     * @return
     */
    /*private Map<String, Object> convert2zh(Map<String, Object> resultMap) {
        Map<String, Object> resultZHMap = new HashMap<>(resultMap.size() + 1);
        for (String key : resultMap.keySet()) {
            switch (key) {
                case "statusCode":
                    String statusCode = ComUtil.safeString(resultMap.get(key));
                    resultZHMap.put("状态码", statusCode);

                    String statusDescription;
                    switch (statusCode) {
                        case "00":
                            statusDescription = "成功";
                            break;
                        case "01":
                            statusDescription = "未找到终端记录";
                            break;
                        case "02":
                            statusDescription = "终端响应超时";
                            break;
                        case "03":
                            statusDescription = "终端不支持该命令";
                            break;
                        default:
                            statusDescription = "未知";
                    }
                    resultZHMap.put("状态说明", statusDescription);
                    break;
                case "temperatureUpper":
                    resultZHMap.put("温度上限", resultMap.get(key));
                    break;
                case "temperatureLower":
                    resultZHMap.put("温度下限", resultMap.get(key));
                    break;
                case "heatSeasonStart":
                    resultZHMap.put("采暖季开始", resultMap.get(key));
                    break;
                case "heatSeasonEnd":
                    resultZHMap.put("采暖季结束", resultMap.get(key));
                    break;
                case "cleanCycle":
                    resultZHMap.put("清洗周期(分)", resultMap.get(key));
                    break;
                case "openDegreeUpper":
                    resultZHMap.put("开度上限", resultMap.get(key));
                    break;
                case "openDegreeLower":
                    resultZHMap.put("开度下限", resultMap.get(key));
                    break;
                case "adjustMode":
                    String adjustModeTxt;
                    int adjustMode = ComUtil.safeInt(resultMap.get(key));
                    switch (adjustMode) {
                        case 0:
                            adjustModeTxt = "面板控制";
                            break;
                        case 1:
                            adjustModeTxt = "远程控制开度";
                            break;
                        case 2:
                            adjustModeTxt = "回水温度控制开度";
                            break;
                        case 3:
                            adjustModeTxt = "进回水温差控制开度";
                            break;
                        default:
                            adjustModeTxt = "未知";
                    }
                    resultZHMap.put("调节模式", adjustModeTxt);
                    break;
                case "adjustValue":
                    resultZHMap.put("目标调节值", resultMap.get(key));
                    break;
                case "autoAdjust":
                    String autoAdjustTxt;
                    int autoAdjust = ComUtil.safeInt(resultMap.get(key));
                    switch (autoAdjust) {
                        case 0:
                            autoAdjustTxt = "启用";
                            break;
                        case 1:
                            autoAdjustTxt = "不启用";
                            break;
                        default:
                            autoAdjustTxt = "未知";
                    }
                    resultZHMap.put("是否启用自动调平", autoAdjustTxt);
                    break;
                case "adjustCycle":
                    resultZHMap.put("调节周期(分)", resultMap.get(key));
                    break;
                case "version":
                    resultZHMap.put("版本号", resultMap.get(key));
                    break;
                case "collectTime":
                    resultZHMap.put("采集时间", resultMap.get(key));
                    break;
                default:
                    log.error("convert2zh: key[{}] match failed", key);
            }
        }
        return resultZHMap;
    }*/

    /**
     * 保存 终端配置参数
     *
     * @param collectorId
     * @param terminalNo
     * @param paramMap
     */
    /*public void saveTerminalConfigParam(Integer collectorId, String terminalNo, Map<String, Object> paramMap) {
        try {
            TerminalVO terminalVO = terminalMapper.getByTerminalNo(terminalNo, collectorId);
            if (terminalVO == null) {
                log.error("terminal[{}], collectorId[{}] not exists", terminalNo, collectorId);
                return;
            }

            int count;

            Integer id = configParamTerminalMapper.getIdByTerminalId(terminalVO.getId());
            if (id == null) {
                // 终端Id
                paramMap.put("terminalId", terminalVO.getId());
                count = configParamTerminalMapper.insert(paramMap);
                if (count <= 0) {
                    log.error("terminal config param insert fail");
                    return;
                }
            } else {
                // 记录Id
                paramMap.put("id", id);
                count = configParamTerminalMapper.update(paramMap);
                if (count <= 0) {
                    log.error("terminal config param update fail");
                    return;
                }
            }
            log.info("terminal config param save ok: {}", JsonUtil.safeJsonStr(paramMap));
        } catch (Exception e) {
            log.error("terminal config param save exception", e);
        }
    }*/

    /**
     * 获取 命令信息
     *
     * @param afn
     * @param fn
     * @return
     */
    public String[] getCmdInfo(String afn, String fn) {
        String[] cmdInfoArr = new String[2];
        switch (afn) {
            case "04":
                switch (fn) {
                    case "f12":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "设置所有终端采集周期";
                        break;
                    case "f16":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "校时";
                        break;
                    case "f18":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "配置下发";
                        break;
                    case "f21":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "采集器升级";
                        break;
                    default:
                        return null;
                }
                break;
            case "05":
                switch (fn) {
                    case "f13":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "读VAC200OC阀门配置";
                        break;
                    case "f17":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "读取版本";
                        break;
                    default:
                        return null;
                }
                break;
            case "0c":
                switch (fn) {
                    case "f30":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "读取实时数据";
                        break;
                    case "f32":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "读取实时数据";
                        break;
                    default:
                        return null;
                }
                break;
            case "0d":
                switch (fn) {
                    case "f17":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "读取最近历史";
                        break;
                    default:
                        return null;
                }
                break;
            case "13":
                switch (fn) {
                    case "f2":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "普开";
                        break;
                    case "f3":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "普关";
                        break;
                    case "f4":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "强制开阀";
                        break;
                    case "f5":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "强制关阀";
                        break;
                    case "f6":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "解锁阀门";
                        break;
                    case "f7":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "强制设温";
                        break;
                    case "f8":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "解锁设温";
                        break;
                    case "f26":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "开度设置";
                        break;
                    case "f27":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "开度范围设置";
                        break;
                    case "f28":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "开度上限设置";
                        break;
                    case "f29":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "取消远程模式";
                        break;
                    case "f30":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "温度范围设置";
                        break;
                    case "f31":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "面板控制阀门";
                        break;
                    case "f32":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "远程控制开度";
                        break;
                    case "f33":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "回温自动控制开度";
                        break;
                    case "f34":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "温差自动控制开度";
                        break;
                    case "f35":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "启用自动调平功能";
                        break;
                    case "f36":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "关闭自动调平功能";
                        break;
                    case "f37":
                        cmdInfoArr[0] = "terminal";
                        cmdInfoArr[1] = "调节周期设定";
                        break;
                    case "f38":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "采暖季设置";
                        break;
                    case "f39":
                        cmdInfoArr[0] = "collector";
                        cmdInfoArr[1] = "设置清洗周期";
                        break;
                    default:
                        return null;
                }
                break;
            default:
                return null;
        }
        return cmdInfoArr;
    }

    /**
     * 获取 命令类型
     *
     * @param afn
     * @param da
     * @param dt
     * @return
     */
    public String getCmdType(String afn, int da, int dt) {
        String commandType = null;
        switch (afn) {
            // 01H
            case "01":
                // F5：采集器复位重启
                if (da == Constant.DA_0 && dt == Constant.DT_4096) {
                    commandType = "collector";
                }
                // F6：恢复出厂设置
                else if (da == Constant.DA_0 && dt == Constant.DT_8192) {
                    commandType = "collector";
                }
                // 匹配失败
                else {
                    return null;
                }
                break;
            // 04H
            case "04":
                // F1：配置
                if (da == Constant.DA_0 && dt == Constant.DT_256) {
                    commandType = "collector";
                }
                // F2：主站IP地址和端口
                else if (da == Constant.DA_0 && dt == Constant.DT_512) {
                    commandType = "collector";
                }
                // F3：采集器编号、心跳周期
                else if (da == Constant.DA_0 && dt == Constant.DT_1024) {
                    commandType = "collector";
                }
                // F7：副站IP地址和端口
                else if (da == Constant.DA_0 && dt == Constant.DT_16384) {
                    commandType = "collector";
                }
                // F10：用户配置参数增删改
                else if (da == Constant.DA_0 && dt == Constant.DT_513) {
                    commandType = "collector";
                }
                // F12：修改所有终端采集周期
                else if (da == Constant.DA_0 && dt == Constant.DT_2049) {
                    commandType = "collector";
                }
                // F16：采集器校时
                else if (da == Constant.DA_0 && dt == Constant.DT_32769) {
                    commandType = "collector";
                }
                // F18：配置信息初始化
                else if (da == Constant.DA_0 && dt == Constant.DT_514) {
                    commandType = "collector";
                }
                // F20：数据解析配置初始化
                else if (da == Constant.DA_0 && dt == Constant.DT_2050) {
                    commandType = "collector";
                }
                // F21：系统升级
                else if (da == Constant.DA_0 && dt == Constant.DT_4098) {
                    commandType = "collector";
                }
                // 匹配失败
                else {
                    return null;
                }
                break;
            // 0CH
            case "0c":
                // F32：读所有终端实时信息
                if (da == Constant.DA_0 && dt == Constant.DT_32771) {
                    commandType = "collector";
                }
                // 匹配失败
                else {
                    return null;
                }
                break;
            // 0DH
            case "0d":
                // F17：读最近一次历史数据（所有）
                if (da == Constant.DA_0 && dt == Constant.DT_258) {
                    commandType = "collector";
                }
                // F20：历史数据初始化
                else if (da == Constant.DA_0 && dt == Constant.DT_2050) {
                    commandType = "collector";
                }
                // F21：删除历史数据（删除某一年所有历史）
                else if (da == Constant.DA_0 && dt == Constant.DT_4098) {
                    commandType = "collector";
                }
                // F24：读所有某时间历史数据
                else if (da == Constant.DA_0 && dt == Constant.DT_32770) {
                    commandType = "collector";
                }
                // 匹配失败
                else {
                    return null;
                }
                break;
            // 13H
            case "13":
                // F2：开阀门
                if (da == Constant.DA_0 && dt == Constant.DT_512) {
                    commandType = "terminal";
                }
                // F3：关阀门
                else if (da == Constant.DA_0 && dt == Constant.DT_1024) {
                    commandType = "terminal";
                }
                // F4：强制开阀
                else if (da == Constant.DA_0 && dt == Constant.DT_2048) {
                    commandType = "terminal";
                }
                // F5：强制关阀
                else if (da == Constant.DA_0 && dt == Constant.DT_4096) {
                    commandType = "terminal";
                }
                // F6：解锁阀门
                else if (da == Constant.DA_0 && dt == Constant.DT_8192) {
                    commandType = "terminal";
                }
                // F7：强制设温
                else if (da == Constant.DA_0 && dt == Constant.DT_16384) {
                    commandType = "terminal";
                }
                // F8：解锁设温
                else if (da == Constant.DA_0 && dt == Constant.DT_32768) {
                    commandType = "terminal";
                }
                // F11：广播设温
                else if (da == Constant.DA_0 && dt == Constant.DT_1025) {
                    commandType = "terminal";
                }
                // F15：广播解锁设温
                else if (da == Constant.DA_0 && dt == Constant.DT_16385) {
                    commandType = "terminal";
                }
                // F18：广播强开
                else if (da == Constant.DA_0 && dt == Constant.DT_514) {
                    commandType = "terminal";
                }
                // F19：广播强关
                else if (da == Constant.DA_0 && dt == Constant.DT_1026) {
                    commandType = "terminal";
                }
                // F20：广播解锁阀门
                else if (da == Constant.DA_0 && dt == Constant.DT_2050) {
                    commandType = "terminal";
                }
                // F26：开度设置
                else if (da == Constant.DA_0 && dt == Constant.DT_515) {
                    commandType = "terminal";
                }
                // F27：开度范围设置
                else if (da == Constant.DA_0 && dt == Constant.DT_1027) {
                    commandType = "terminal";
                }
                // F28：开度上限设置
                else if (da == Constant.DA_0 && dt == Constant.DT_2051) {
                    commandType = "terminal";
                }
                // F29：取消远程模式
                else if (da == Constant.DA_0 && dt == Constant.DT_4099) {
                    commandType = "terminal";
                }
                // F30：温度范围设置
                else if (da == Constant.DA_0 && dt == Constant.DT_8195) {
                    commandType = "terminal";
                }
                // F31：面板控制阀门
                else if (da == Constant.DA_0 && dt == Constant.DT_16387) {
                    commandType = "terminal";
                }
                // F32：远程控制开度
                else if (da == Constant.DA_0 && dt == Constant.DT_32771) {
                    commandType = "terminal";
                }
                // F33：回温自动控制开度
                else if (da == Constant.DA_0 && dt == Constant.DT_260) {
                    commandType = "terminal";
                }
                // F34：温差自动控制开度
                else if (da == Constant.DA_0 && dt == Constant.DT_516) {
                    commandType = "terminal";
                }
                // F35：启用自动调平功能
                else if (da == Constant.DA_0 && dt == Constant.DT_1028) {
                    commandType = "terminal";
                }
                // F36：关闭自动调平功能
                else if (da == Constant.DA_0 && dt == Constant.DT_2052) {
                    commandType = "terminal";
                }
                // F37：调节周期设定
                else if (da == Constant.DA_0 && dt == Constant.DT_4100) {
                    commandType = "terminal";
                }
                // F38：采暖季设置
                else if (da == Constant.DA_0 && dt == Constant.DT_8196) {
                    commandType = "terminal";
                }
                // F39：设置清洗周期
                else if (da == Constant.DA_0 && dt == Constant.DT_16388) {
                    commandType = "terminal";
                }
                // 匹配失败
                else {
                    return null;
                }
                break;
            default:
                log.error("execute afn[{}] match fail", afn);
        }
        return commandType;
    }

    /**
     * 通知调用
     *
     * @param terminalData
     * @param varMap
     * @param collectStatus
     * @param collectTime
     * @param terminalState
     * @param panelState
     * @param terminalFault
     */
    /*public void notifyInvoke(terminalData terminalData, Map<String, Object> varMap, Integer collectStatus, String collectTime,
                             String terminalState, String panelState, String terminalFault) {
        ThreadPoolHelper.getCachedInstance().execute(() -> {
            String allChildIdStr;

            // 北明天时：实时数据
            if (PropertyValue.NOTIFY_BEI_MING) {
                allChildIdStr = customerMapper.getAllChildIdByName(Constant.CUSTOMER_BEI_MING);
                if (ComUtil.isContain(allChildIdStr, terminalData.getCustomerId())) {
                    Map<String, Object> dataMap = new HashMap<>(3);
                    dataMap.put("collectorNo", terminalData.getCollectorNo());
                    dataMap.put("macAddr", terminalData.getMacAddr());
                    dataMap.put("level", terminalData.getTerminalLevel());
                    dataMap.put("type", terminalData.getTerminalType());
                    dataMap.put("customerTxt", terminalData.getCustomerTxt());
                    dataMap.put("varMap", varMap);
                    dataMap.put("collectStatus", collectStatus);
                    dataMap.put("collectTime", collectTime);
                    dataMap.put("terminalState", terminalState);
                    dataMap.put("panelState", panelState);
                    dataMap.put("terminalFault", terminalFault);
                    RedisHelper.send("beiming/data-change", JsonUtil.safeJsonStr(dataMap));

                    log.info("notifyInvoke beiming ok: data[{}]", JsonUtil.safeJsonStr(dataMap));
                }
            }
        });
    }*/
}
